﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class UserControls_AnalysisMenu : System.Web.UI.UserControl
{

  private Companies c = new Companies();
  private StringBuilder cstext2 = new StringBuilder();
  DataTable dt = new DataTable();
  
  protected void Page_Load(object sender, EventArgs e)
  {

    if (!IsPostBack)
    {
      BindGridView("DNK");
      lblCountry.Text = "Danske selskaber";
    }
    
  }

  protected void GridViewCompanies_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    dt = SessionDataTable;
    GridViewCompanies.PageIndex = e.NewPageIndex;
    GridViewCompanies.DataSource = dt;
    GridViewCompanies.DataBind();
  }

  protected void GridViewCompanies_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    //Set column width to 50%
    e.Row.Cells[0].Width = Unit.Percentage(50);
  }

  protected void TextBox1_TextChanged(object sender, EventArgs e)
  {
    Company c = new Company();
    c.GetCompanyIdByName(TextBox1.Text);
    if (c.Id != null)
    {
      HyperLink1.NavigateUrl = "/Aktieanalyser/Analyse?id=" + c.Id;
      cstext2.Append("<script type=\"text/javascript\"> $(function () {ClickOnLink()}); </script>");
    }
    else
    {
      HyperLink1.NavigateUrl = "/Aktieanalyser/AnalyseIkkeFundet";
      cstext2.Append("<script type=\"text/javascript\"> $(function () {ClickOnLink()}); </script>");
    }
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Click", cstext2.ToString());
  }

  protected void SearchButton_Click(object sender, EventArgs e)
  {
    Company c = new Company();
    c.GetCompanyIdByName(TextBox1.Text);
    if (c.Id != null)
    {
      HyperLink1.NavigateUrl = "/Aktieanalyser/Analyse?id=" + c.Id;
      cstext2.Append("<script type=\"text/javascript\"> $(function () {ClickOnLink()}); </script>");
    }
    else
    {
      HyperLink1.NavigateUrl = "/Aktieanalyser/AnalyseIkkeFundet";
      cstext2.Append("<script type=\"text/javascript\"> $(function () {ClickOnLink()}); </script>");
    }
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Click", cstext2.ToString());

  }

  protected void DNK_Click(object sender, ImageClickEventArgs e)
  {
    BindGridView("DNK");
    lblCountry.Text = "Danske selskaber";
  }

  protected void USA_Click(object sender, ImageClickEventArgs e)
  {
    BindGridView("USA");
    lblCountry.Text = "Amerikanske selskaber";
  }

  protected void SWE_Click(object sender, ImageClickEventArgs e)
  {
    BindGridView("SWE");
    lblCountry.Text = "Svenske selskaber";
  }

  protected void NOW_Click(object sender, ImageClickEventArgs e)
  {
    BindGridView("NOW");
    lblCountry.Text = "Norske selskaber";
  }

  protected void GRB_Click(object sender, ImageClickEventArgs e)
  {
    BindGridView("GBR");
    lblCountry.Text = "Britiske selskaber";
  }

  protected void FIN_Click(object sender, ImageClickEventArgs e)
  {
    BindGridView("FIN");
    lblCountry.Text = "Finske selskaber";
  }

  protected void NED_Click(object sender, ImageClickEventArgs e)
  {
    BindGridView("NED");
    lblCountry.Text = "Hollanske selskaber";
  }

  protected void GER_Click(object sender, ImageClickEventArgs e)
  {
    BindGridView("DEU");
    lblCountry.Text = "Tyske selskaber";
  }

  private void BindGridView(string country)
  {
    dt.Load(c.GetCompaniesByCountry(country));
    GridViewCompanies.DataSource = dt;
    GridViewCompanies.DataBind();
    SessionDataTable = dt;
  }

  private DataTable SessionDataTable
  {
    get { return (DataTable)Session["SessionDataTable"]; }
    set { Session["SessionDataTable"] = value; }
  }
}
